package com.example.springLearning.part10.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Optional;

@RestController
@RequestMapping("/spring/part10")
@Slf4j
public class Spring10Controller {

    @RequestMapping(path = "/hi/{name}", method = RequestMethod.GET)
    public String notifyEvent(@PathVariable("name") String name) {
        return name;
    }

    @RequestMapping(path = "/hi/sss/{name}", method = RequestMethod.GET)
    public String notifyEvent1(@PathVariable("name") String name) {
        System.out.println("another method");
        return name;
    }

    @RequestMapping(path = "/hi1/**", method = RequestMethod.GET)
    public String notifyEvent2(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        return requestURI.split("/hi1/")[1];
    }

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @RequestMapping(path = "/hi2/**", method = RequestMethod.GET)
    public String notifyEvent3(HttpServletRequest request) {
        String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        log.info(path);
        String matchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        log.info(matchPattern);
        return antPathMatcher.extractPathWithinPattern(matchPattern,path);
    }

    /**
     * 必须显式在 @RequestParam 中指定请求参数名
     * @param name
     * @return
     */
    @RequestMapping(path = "/hhi1", method = RequestMethod.GET)
    public String hi1(@RequestParam("name") String name) {
        return name;
    }

    @RequestMapping(path = "/hhi2", method = RequestMethod.GET)
    public String hi2(@RequestParam String name) {
        return name;
    }

    @RequestMapping(path = "/hi4", method = RequestMethod.GET)
    public String hi4(@RequestParam("name") String name, @RequestParam(value = "address", defaultValue = "no address") String address) {
        return name + " : " + address;
    }

    @RequestMapping(path = "/hi5", method = RequestMethod.GET)
    public String hi5(@RequestParam("name") String name, @RequestParam(value = "address", required = false) String address) {
        return name + " : " + address;
    }

    @RequestMapping(path = "/hi6", method = RequestMethod.GET)
    public String hi6(@RequestParam("name") String name, @RequestParam(value = "address") @Nullable String address) {
        return name + " : " + address;
    }

    @RequestMapping(path = "/hi7", method = RequestMethod.GET)
    public String hi7(@RequestParam("name") String name, @RequestParam(value = "address") Optional address) {
        return name + " : " + address;
    }

    @RequestMapping(path = "/hi8", method = RequestMethod.GET)
    public String hi8(@RequestParam("name") String name, @RequestParam(value = "date") Date date) {
        return name + " : " + date;
    }

    @RequestMapping(path = "/hi9", method = RequestMethod.GET)
    public String hi9(@RequestParam("name") String name, @RequestParam(value = "date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date) {
        return name + " : " + date;
    }
}
